#include "dbhelper.h"

dbhelper::dbhelper():m_db(QSqlDatabase::addDatabase("QODBC"))
{

}

bool dbhelper::init_db(){
    m_db.setHostName("127.0.0.1");
    m_db.setPort(3306);
    m_db.setUserName("root");
    m_db.setPassword("wb412412");
    m_db.setDatabaseName("MySQL57");

    if(m_db.open()){
        return true;
    }else{
        return false;
    }
}

QList<User> dbhelper::getAllUsers(){
    QList<User> users;
    QSqlQuery query;
    QString strsql = QString("select * from user;");

    if(!query.exec(strsql)){
        qDebug() << "execute query error;" << query.lastError().text();
        return users;
    }else{
        qDebug() << "execute query success.";
    }

    while(query.next()){
        QString id = query.value("id").toString();
        QString name = query.value("name").toString();
        QString password = query.value("password").toString();
        float total_amount = query.value("total_amount").toFloat();
        float consume_amount = query.value("consume_amount").toFloat();

        users.append(User(id, name, password, total_amount, consume_amount));
    }

    return users;

}
